/**
 * @author a1
 * v0.1
 * http://code.google.com/p/waggledance
 */
var waggleoutside = 'waggledance is installed but your account doesn\'t allow you to use it outside of the Situation Room.';
var wmiss1 = ['VPN 213.24.76.133', 'ups shutdown outlet4', 'VPN exit', 'VPN 213.24.76.22', 'setconfig transac remotebackup 137.227.232.150', 'remotebackup initiate'];
var wmiss2 = ['Reflex test hack', 'Investigation analysis', 'Maths genius mode', 'Morse test hack', 'Comms test hack'];
var miss2invest = ['Leonid Ilyich Brezhnev', 'jonestown','042'];

function GHook() {

	// Initialize the reference to this object
	var me = this;

	// UI 
    me.setupWindow = function () {
			// css
            $('<style type"text/css">.wagmissiontitle a {font-weight: bold;}.wagmissiontitle ul li {width: 250px;}#waggledance ul{width: 290px;} #waggledance{position:absolute;top:40%;left:-310px;padding:6px;margin:0;list-style:none;width:320px!important;background-color:#fff;border:1px solid #000;z-index:2100;}#waggledance *{background:transparent!important;border:0!important;color:#000!important;font-family:consolas!important;font-size:10px!important;font-weight:normal!important;letter-spacing:normal!important;line-height:1!important;list-style-type:none!important;margin:0!important;padding:0!important;text-decoration:none!important;text-indent:0!important;text-transform:none!important;word-spacing:0!important;z-index:2100!important;}</style>').appendTo('body');
            //html
            $('<div id="waggledance"><ul id="wagglemissions"></ul><div style="clear: left;"></div><div id="waggledance-tab"><a href="javascript:;"><img src="http://i.imgur.com/gAe1P.jpg" height="21" width="22" border="0" style="float: right;" /></a></div></div>').appendTo('body');
			$('#waggledance-tab a img').click(function () {
				me.toggleSetupWindow();
			});
			me.makeMissions();
	};

	me.toggleSetupWindow = function () {
			if (parseInt($('#waggledance').css('left'), 10) === 0) {
				$('#waggledance').animate({ left: -310 }, 200);
			}
			else {
				$('#waggledance').animate({ left: 0 }, 200);
			}
    };

    me.term = function (input) {
        if ($('#tconsole')) {
            $('#tconsole').terminal().exec(input);
        } else { 
            // error handling
        }
    };
    
    me.makeMissions = function() {
    	//mission1
    	$('#wagglemissions').append('<li class="wagmissiontitle" id="wagglemission1"><a href="#" onclick="Javascript:window.ghook.toggle(\'mission1\');">Mission 1</a><ul style="display:none;"></ul></li>');
	   	// comment / how to
	   	$('#wagglemission1 ul').append('<li class="wagmissiondesc"><br /><i>Start the mission, open the console and login if required. Then clic the [Start] link while the console is open. You can also use the checkboxes to stop the script where you want, waggledance will stop the process before the checked line.</i></br /><br /></li>');
	   	for (var i in wmiss1) {
    		$('#wagglemission1 ul').append('<li>- <input type="checkbox" class="wdmiss1' + i + '" />' + wmiss1[i] + '</li>');
    	}
    	$('#wagglemission1 ul').append('<li><br /><a href="#" onclick="Javascript:window.ghook.startMission1();">[START]</a><br /><br /></li>');
    	
    	//mission2
    	$('#wagglemissions').append('<li class="wagmissiontitle" id="wagglemission2"><a href="#" onclick="Javascript:window.ghook.toggle(\'mission2\');">Mission 2</a><ul style="display:none;"></ul></li>');
    	$('#wagglemission2 ul').append('<li class="wagmissiondesc"><br /><i>To use waggerdance on the mission 2, you just need to open the mission window (reflex, morse, ...) by clicking on a tv ; then, you just jave to use the corresponding link in waggerdance!<br /><b>NOTE : Do not start the mission! The script will take care of everything</b></i></br /><br />NOTE2 : investigation is resolved one step at a time, just open the window and click the current step.<br /><br /></li>');
	   	
    	
    	$('#wagglemission2 ul').append('<li>- <a href="#" onclick="Javascript:window.ghook.stdHack(\'REFLEX\');">Reflex hack</a></li>');
    	$('#wagglemission2 ul').append('<li>- <a href="#" onclick="Javascript:window.ghook.stdHack(\'MORSE\');">Morse hack</a></li>');
    	$('#wagglemission2 ul').append('<li>- <a href="#" onclick="Javascript:window.ghook.stdHack(\'ARITHMETICS\');">Math genius</a></li>');
    	$('#wagglemission2 ul').append('<li>- <a href="#" onclick="Javascript:window.ghook.stdHack(\'COMMUNICATION\');">Comms hack</a></li>');
    	//$('#wagglemission2 ul').append('<li>- <a href="#" onclick="Javascript:window.ghook.investigation();">Investigation analysis</a></li>');
    	$('#wagglemission2 ul').append('<li>- Investigation : <a href="#" onclick="Javascript:window.ghook.investigate(1)">Step1</a> - <a href="#" onclick="Javascript:window.ghook.investigate(2)">Step2</a> - <a href="#" onclick="Javascript:window.ghook.investigate(3)">Step3</a></li>');
    }
    
    me.startMission1 = function() {
    	for (var i in wmiss1) {
    		if(!$("#wagglemission1 input.wdmiss1" + i).attr('checked')) { me.term(wmiss1[i]); }
    		else { break; }// handle debug checkbox
    	}
    }
    
    me.stdHack = function(title) {
    	if ($("#lightbox-content").contents().find('h3.title').text() == title) {
    		if($("#lightbox-content").contents().find('#start').val() == "Start") { // not started > do it.
    			document.getElementById('lightbox-content').contentWindow.startgame();
    		} else {
    			alert('The mission is started... aborting. Please use waggerdance without STARTING the mission.');
    			return;
    		}
    		$("#lightbox-content").contents().find('#score').val(30000); // rocks
    		if (title == "REFLEX") document.getElementById('lightbox-content').contentWindow['counter'] = 0;
	    	document.getElementById('lightbox-content').contentWindow['time'] = 1; // set time to 0
	    	
    	} else {alert("Please open the mission window before executing the waggledance.");}
    	
    }

    me.investigate = function(step) {
    	 if ($("#lightbox-content").contents().find('h3').text() == 'INVESTIGATION') {
    	    	//step1
    	    	$("#lightbox-content").contents().find('input.answer').val(miss2invest[step-1]);
    	    	$("#lightbox-content").contents().find('form').submit();	
    	    		
    	 } else {alert("Please open the mission window before executing the waggledance.");}
    }
    
    me.toggle = function(elem) {
    	if ($('#waggle' + elem + ' ul').css('display') == 'block') { $('#waggle' + elem + ' ul').css('display', 'none'); }
    	else { $('#waggle' + elem + ' ul').css('display', 'block'); }
    }

}

// are we home ? 
function checkHome() { // need to redo this
	//debugger;
	console.log('checkhome');
	if (typeof(window.$) === "undefined") {
		console.log('undef');
		alert(waggleoutside);
		return false;
	}
	else {
		console.log('jqfound');
		if ($('#tconsole').length > 0) {console.log('tconsole'); window.ghook.setupWindow(); return true;}//window.ghook.setupWindow();}
		if ($('#mt').length > 0)  { console.log('mt'); alert('waggleDance is installed ! You can go now use it from Situation room.'); return false; }//window.ghook = null; }
		else {alert(waggleoutside); return false;}
	}
}

// Initiate the script, if it already is initiated, toggle setup window
if (typeof (window.ghook) === "undefined") {
	window.ghook = new GHook();
    if (checkHome()) { window.ghook.toggleSetupWindow(); }
}
else {
	if ($('#tconsole').length > 0) {window.ghook.toggleSetupWindow();} // only inside the sit room
}

